package edu.tjut.tjutcommunity.controller;


import org.springframework.core.io.ClassPathResource;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.InputStream;

@Controller
@RequestMapping(value = "/image")
public class ImageController {
    @RequestMapping(value = "/get", produces = MediaType.IMAGE_JPEG_VALUE)
    @ResponseBody
    public byte[] getImage() throws IOException {
        ClassPathResource resource = new ClassPathResource("static/test.jpg");
        InputStream inputStream = resource.getInputStream();
        int available = inputStream.available();
        byte[] bytes = new byte[available];
        int read = 0;
        int pos = 0;
        byte[] b = new byte[1024];
        while ((read = inputStream.read(b)) != -1) {
            System.arraycopy(b,0,bytes,pos,read);
            pos+=read;
        }
        return bytes;
    }
}